package login;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.*;

public final class LoginManager extends HttpServlet{
   private final static String LOGIN_NAME_ATTRIBUTE ="loginName";
   private final static String PASS_NAME_ATTRIBUTE ="passId";

   private LoginManager() {}
   public static boolean login(HttpServletRequest request,String loginName, String pass) throws IOException {
      HttpSession session = request.getSession(true);
      
      Usuarios users = new Usuarios();
      boolean authenticated = false;
      Map errors = new HashMap();
      
      if(users.registered(loginName)){
              authenticated = users.login(loginName,pass);

              if(authenticated){
                session.setAttribute(LOGIN_NAME_ATTRIBUTE, loginName);
                //session.setAttribute(PASS_NAME_ATTRIBUTE, pass);
              }else{
                errors.put("password", "Wrong password");
              }
      }else{
          errors.put("loginName", "User not registered");
      }
      if(!errors.isEmpty()) request.setAttribute("errors", errors);
      session.setAttribute("authenticated", authenticated);
      return authenticated;

   }
   
   /*public final static void register(HttpServletRequest request,String loginName, Password pass) {
      HttpSession session = request.getSession(true);
      
      String usrFile = "./login/usuarios";
      Usuarios users = new Usuarios(usrFile);
      //users.setUsr();
      if(users.login(loginName,pass)){
        session.setAttribute(LOGIN_NAME_ATTRIBUTE, loginName);
        session.setAttribute(PASS_NAME_ATTRIBUTE, pass);
      }
   }*/
   
   public final static void logout(HttpServletRequest request) {
      HttpSession session = request.getSession(false);
      if (session != null) {
         session.invalidate();
      }
   }
   public final static String getLoginName(HttpServletRequest request) {
     HttpSession session = request.getSession(false);
     if (session == null) {
        return null;
     } else {
     // deprecated, sustituida por getAttribute
     return (String) session.getAttribute(LOGIN_NAME_ATTRIBUTE);
     }
   }
   
   public final static boolean authenticated(HttpServletRequest request) {
     HttpSession session = request.getSession(false);
     if (session == null) {
        return false;
     } else {
     // deprecated, sustituida por getAttribute
         boolean res;
         if ((session.getAttribute("authenticated") == null) || (!(Boolean)session.getAttribute("authenticated"))){
             res = false;
         }else{
             res=true;
         }
        return (Boolean) res;
     }
   }
   
   /*public final static void reinit(HttpServletRequest request){
       HttpSession session = request.getSession(false);
       Enumeration<String> e = session.getAttributeNames();
       while(e.hasMoreElements()){
           String elem = e.nextElement();
           if(e.equals("downloads")){
               session.setAttribute(elem, 0);
           }
           session.removeAttribute(elem);
       }
   }*/
}